package com.itrip.biz.controller;


import com.itrip.beans.pojo.User;
import com.itrip.beans.pojo.UserLinkUser;
import com.itrip.biz.service.UserLinkUserService;
import com.itrip.utils.dto.Dto;
import com.itrip.utils.vo.DtoUtil;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 周勉
 * @date: 2020/2/28 16:09
 */
@RestController
@RequestMapping("/biz/api/userinfo")
public class UserInfoController {
    @Autowired
    private UserLinkUserService userLinkUserService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 查询旅客信息
     * @param linkUserName  旅客姓名
     * @return
     */
    @PostMapping("/queryuserlinkuser")
    public Dto<UserLinkUser> queryUserLinkUser(
            @ApiParam(name = "linkUserName",value = "旅客对象",required = false)
            @RequestBody UserLinkUser linkUserName,
            HttpServletRequest request){

        List<UserLinkUser> userLinkUserList=new ArrayList<>();
        //获取token
        String token=request.getHeader("token");
        //判断是否登录或登录是否过期
       if(token!=null){
           //根据token 获取用户Id
            Integer UserId=Integer.parseInt(stringRedisTemplate.opsForValue().get(token));
            Map<String,Object> param=new HashMap<>();
            param.put("userId",UserId);
            param.put("linkUserName",linkUserName.getLinkUserName());
            try {
                userLinkUserList=userLinkUserService.getItripUserLinkUserListByMap(param);
                return DtoUtil.returnSuccess("获取常用联系人信息成功",userLinkUserList);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("获取常用联系人信息失败");
            }
        }else{
            return DtoUtil.returnFail("token失效，请重新登录");
        }

    }

    /**
     *  新增旅客信息
     * @param userLinkUser  旅客对象
     * @return
     */
    @PostMapping("/adduserlinkuser")
    public Dto addUserLinkUser(@RequestBody UserLinkUser userLinkUser,HttpServletRequest request){
        //获取token
        String token=request.getHeader("token");
        //判断是否登录或登录是否过期
        if(token!=null){
            //根据token 获取用户Id
            Integer UserId=Integer.parseInt(stringRedisTemplate.opsForValue().get(token));
            userLinkUser.setUserId(UserId);
            userLinkUser.setCreatedBy(Long.valueOf(UserId));
            if(userLinkUserService.save(userLinkUser)){
                return DtoUtil.returnSuccess("新增常用联系人成功");
            }else{
                return DtoUtil.returnFail("新增常用联系人失败");
            }
        }else {
            return DtoUtil.returnFail("token失效，请重新登录");
        }



    }

    @PostMapping("/modifyuserlinkuser")
    public Dto modifyUserLinkUser(@RequestBody UserLinkUser userLinkUser,HttpServletRequest request){
        //获取token
        String token=request.getHeader("token");
        //判断是否登录或登录是否过期
        if(token!=null){
            //根据token 获取用户Id
           Integer UserId=Integer.parseInt(stringRedisTemplate.opsForValue().get(token));
            userLinkUser.setModifiedBy(Long.valueOf(UserId));
            if(userLinkUserService.updateById(userLinkUser)){
                return DtoUtil.returnSuccess("修改常用联系人成功");
            }else{
                return DtoUtil.returnFail("修改常用联系人失败");
            }
        }else {
            return DtoUtil.returnFail("token失效，请重新登录");
        }
    }


    /**
     *  删除 旅客信息
     * @param ids   旅客ID
     * @return 返回是否删除失败
     */
    @GetMapping("/deluserlinkuser")
    public Dto delUserLinkUser(Integer[] ids,HttpServletRequest request){


        //获取token
        String token=request.getHeader("token");
        //判断是否登录或登录是否过期
        if(token!=null){
            //根据token 获取用户Id
            Integer UserId=Integer.parseInt(stringRedisTemplate.opsForValue().get(token));
            if (ids!=null){
                List<Integer> idList=java.util.Arrays.asList(ids);
                if(userLinkUserService.removeByIds(idList)){
                    return DtoUtil.returnSuccess("删除常用联系人成功");
                }else {
                    return DtoUtil.returnFail("删除联系人失败");
                }
            }else{
                return DtoUtil.returnFail("请选择要删除的常用联系人");
            }
        }else {
            return DtoUtil.returnFail("token失效，请重新登录");
        }
    }
}
